package com.caifeng.myapplication;


import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_main);
        initView();
        getFragment();
    }

    private RadioGroup radioGroup;
    private FrameLayout frameLayout;

    private void initView() {

        radioGroup = findViewById(R.id.rg);
        frameLayout = findViewById(R.id.f1);

        //radiogroup使用
        radioGroup.setOnCheckedChangeListener(this);

    }

    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;

    private void getFragment() {

        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();

        addFragment(oneFragment);
        addFragment(twoFragment);
        addFragment(threeFragment);
        addFragment(fourFragment);

//        hideFragment(oneFragment);
        hideFragment(twoFragment);
        hideFragment(threeFragment);
        hideFragment(fourFragment);

    }

    /**
     * 创建容器
     *
     * @param fragment
     */
    private void addFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.f1, fragment);
        fragmentTransaction.commit();

    }

    /**
     * 显示碎片
     *
     * @param fragment
     */

    private void showFragment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction().show(fragment).commit();
    }

    /**
     * 隐藏碎片
     *
     * @param fragment
     */
    private void hideFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().hide(fragment).commit();
    }

    public void hFa() {
        hideFragment(oneFragment);
        hideFragment(twoFragment);
        hideFragment(threeFragment);
        hideFragment(fourFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_one:
                hFa();
                showFragment(oneFragment);
                break;
            case R.id.rb_two:
                hFa();
                showFragment(twoFragment);
                break;
            case R.id.rb_three:
                hFa();
                showFragment(threeFragment);
                break;
            case R.id.rb_four:
                hFa();
                showFragment(fourFragment);
                break;
        }
    }
}
